/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pacificlms.MainPanel;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import pacificlms.LoggedMember;
import pacificlms.MasterConnection;
import pacificlms.Verifiable.Database.Member;

//fixme logout does not clear tabs
//fixme member does not autogenerate
/**
 * ActionLogin is the panel that displays when the user wishes to login
 * ActionLogin will close if the user logs in or cancels login
 * ActionLogin can display the terms/service rules panel
 * ActionLogin can display the Member Catalog with an empty user
 * @author Yasir Tanveer + Brian Hume
 */
public class ActionLogin extends CentralUIPanel {

    /**
     * Creates new form ActionLogin
     */
    public ActionLogin() {
        super(null);
        initComponents();
    }
    
    @Override
    public String TabName()
    {
        return "Login";
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel20 = new javax.swing.JLabel();
        btnCreateAccount = new javax.swing.JButton();
        btnTerms = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel22 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        txtLibraryCard = new javax.swing.JTextField();
        jLabel24 = new javax.swing.JLabel();
        txtPassword = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();
        btnForgotPassword = new javax.swing.JButton();

        setLayout(null);

        jLabel20.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel20.setText("New to LMS ?");
        add(jLabel20);
        jLabel20.setBounds(320, 0, 83, 35);

        btnCreateAccount.setFont(new java.awt.Font("Tahoma", 0, 10));
        btnCreateAccount.setText("CREATE AN ACCOUNT");
        btnCreateAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateAccountjButton9ActionPerformed(evt);
            }
        });
        add(btnCreateAccount);
        btnCreateAccount.setBounds(406, 8, 200, 21);

        btnTerms.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnTerms.setText(" Terms & Privacy");
        btnTerms.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTermsjButton9ActionPerformed(evt);
            }
        });
        add(btnTerms);
        btnTerms.setBounds(10, 500, 150, 21);

        jLabel22.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel22.setText("Library Card #");

        jLabel21.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel21.setText("Login");

        jLabel24.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel24.setText("Password");

        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginjButton9ActionPerformed(evt);
            }
        });

        btnForgotPassword.setText("Forgot Password ?");
        btnForgotPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnForgotPasswordjButton9ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(txtPassword)
                    .addComponent(jLabel21, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLibraryCard, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel22, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
                    .addComponent(jLabel24, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnForgotPassword, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                    .addComponent(btnLogin, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel21, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(jLabel22, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(txtLibraryCard, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(jLabel24, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(btnLogin)
                .addGap(11, 11, 11)
                .addComponent(btnForgotPassword)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1);
        jPanel1.setBounds(147, 153, 306, 252);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Creates a Member Catalog allowing the person to join the library
     */
    private void btnCreateAccountjButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateAccountjButton9ActionPerformed
        MainPanel.GetMainPanel().AddCentralUIPanel(new InformationMember(null));
    }//GEN-LAST:event_btnCreateAccountjButton9ActionPerformed

    /**
     * Attempts to log in the member
     */
    void Login()
    {
        String LibraryCard = txtLibraryCard.getText();
        String Password = txtPassword.getText();
        
        if (Member.ValidateLogin(LibraryCard, Password))
        {
            this.Close();
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Unable to login\nCheck that you typed your Library card # and password correctly", "Unable to login", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    private void btnLoginjButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginjButton9ActionPerformed
        Login();
    }//GEN-LAST:event_btnLoginjButton9ActionPerformed

    /**
     * Email's the user their password (does not change it)
     */
    void ForgotPassword()
    {
        String LibraryCard = txtLibraryCard.getText();
        //fixme this is a potential security risk
        Member Memb = Member.FindMember(LibraryCard, null);
        String Email = Memb.Email(null);
        if (Email != null && !Email.equals(""))
        {
            String Message = "Click yes if you are " + Memb.Name(null) + " and\nwish to have your password emailed to " + Memb.Email(null) + "\n";
            int Return = JOptionPane.showConfirmDialog(null, Message, "Confirmation request", JOptionPane.YES_NO_OPTION);
            
            if (Return == JOptionPane.YES_OPTION)
            {
                Memb.SendPasswordEmail();
            }
        }
    }
    
    private void btnForgotPasswordjButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnForgotPasswordjButton9ActionPerformed
        ForgotPassword();
    }//GEN-LAST:event_btnForgotPasswordjButton9ActionPerformed

    /**
     * Displays the terms/service rules for joining/using the library
     */
    void DisplayTerms()
    {
        MainPanel.GetMainPanel().AddCentralUIPanel(new TermsConditions());
    }
    
    private void btnTermsjButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTermsjButton9ActionPerformed
        DisplayTerms();
    }//GEN-LAST:event_btnTermsjButton9ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCreateAccount;
    private javax.swing.JButton btnForgotPassword;
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnTerms;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtLibraryCard;
    private javax.swing.JPasswordField txtPassword;
    // End of variables declaration//GEN-END:variables
}
